home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / comms / non-internet / samba / source / smbrun.c < prev    next >
C/C++ Source or Header  |  1996-06-26  |  2KB  |  97 lines

  1. /* 
  2.    Unix SMB/Netbios implementation.
  3.    Version 1.9.
  4.    external program running routine
  5.    Copyright (C) Andrew Tridgell 1992-1995
  6.    
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 2 of the License, or
  10.    (at your option) any later version.
  11.    
  12.    This program is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.    
  17.    You should have received a copy of the GNU General Public License
  18.    along with this program; if not, write to the Free Software
  19.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21.  
  22. #include "includes.h"
  23.  
  24.  
  25. /*******************************************************************
  26. close the low 3 fd's and open dev/null in their place
  27. ********************************************************************/
  28. static void close_fds(void)
  29. {
  30.   int fd;
  31.   int i;
  32.   close(0); close(1); close(2);
  33.   /* try and use up these file descriptors, so silly
  34.      library routines writing to stdout etc won't cause havoc */
  35.   for (i=0;i<3;i++) {
  36.     fd = open("/dev/null",O_RDWR,0);
  37.     if (fd < 0) fd = open("/dev/null",O_WRONLY,0);
  38.     if (fd != i) return;
  39.   }
  40. }
  41.  
  42.  
  43. /*
  44. This is a wrapper around the system call to allow commands to run correctly 
  45. as non root from a program which is switching between root and non-root 
  46.  
  47. It takes one argument as argv[1] and runs it after becoming a non-root
  48. user
  49. */
  50. int main(int argc,char *argv[])
  51. {
  52.   close_fds();
  53.  
  54.   if (getuid() != geteuid())
  55.     {
  56.       int uid,gid;
  57.       
  58.       if (getuid() == 0)
  59.     uid = geteuid();
  60.       else
  61.     uid = getuid();
  62.       
  63.       if (getgid() == 0)
  64.     gid = getegid();
  65.       else
  66.     gid = getgid();
  67.       
  68. #ifdef USE_SETRES
  69.       setresgid(0,0,0);
  70.       setresuid(0,0,0);
  71.       setresgid(gid,gid,gid);
  72.       setresuid(uid,uid,uid);      
  73. #else      
  74.       setuid(0);
  75.       seteuid(0);
  76.       setgid(gid);
  77.       setegid(gid);
  78.       setuid(uid);
  79.       seteuid(uid);
  80. #endif
  81.  
  82.       if (getuid() != uid)
  83.     return(3);
  84.     }
  85.  
  86.   if (geteuid() != getuid())
  87.     return(1);
  88.  
  89.   if (argc < 2)
  90.     return(2);
  91.  
  92.   /* this is to make sure that the system() call doesn't run forever */
  93.   alarm(30);
  94.  
  95.   return(system(argv[1]));
  96. }
  97.